<?php
ob_start();
date_default_timezone_set ( 'Europe/Paris' );
setlocale(LC_TIME, "fr", "FR", "fr_FR", "fr_FR.ISO8859-1");
include_once 'private/path.conf';
USE_LIB('template');
USE_LIB('page');
include_once FILE_ROOT . 'stats/logs.class.php5';

define( 'PAGE_ROOT', SITE_ROOT );

class RSS extends Page
	{
	public function parseTemplate( $tpl )
		{
		parent::parseTemplate( $tpl );
		$this->reset();
		$this->fillWith();
		$this->order();
		$this->parseRSS( $tpl );
		}
		
	protected function build() {}
	protected function getPageUrl() {}
	protected function getBreadcrumbTitle() {}
	protected function getPageParent() {}
	protected function getTemplateFilename() {return TPL_ROOT.'rss.tpl';}
	protected function order()
		{
		krsort( $this->m_rss_elements );
		}
		
	protected function reset()
		{
		$this->m_rss_elements = array();
		}
		
	protected function addElement( $elt )
		{
		$this->m_rss_elements[ (string)$elt['date'] ] = $elt;
		}
		
	protected function parseRSS( &$tpl )
		{
		$tpl->setVarDate( 'GENERAL_TODAY', time() );
		$tpl->startLoop('EACH_ELEMENTS');
		foreach( $this->m_rss_elements as $elt )
			{
			$tpl->newLoop('EACH_ELEMENTS');
			$tpl->setVar( 'ELEMENT_URL', $elt['url'] );
			$tpl->setVar( 'ELEMENT_TITLE', $elt['title'] );
			$tpl->setVar( 'ELEMENT_TEXT', $elt['text'] );
			$tpl->setVarDate( 'ELEMENT_DATE', $elt['date'] );
			}
		$tpl->endLoop('EACH_ELEMENTS');
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Cache-Control: private",false);
		header("Content-Type: text/xml");
		$html = $tpl->getHTML();
		header("Content-Length: ".strlen($html));
		echo $html;
		exit();
		}
		
	protected function fillWith()
		{
		USE_APP('film');
		$a = new Film;
		$a->setOrder('f_date_sortie desc');
		$a->perPage(10,$this);
		$as = $a->getElementsByParams();
		foreach( $as as $elt )
			{
			$this->addElement( array(
				'url' => WWW_ROOT . 'films',
				'title' => $elt['f_title'],
				'text' => $elt['f_synopsis'],
				'date' => $elt['f_date_sortie']
				) );
			}
		}
	}

function processor( $page )
	{
	$content = '';
	$tpl = new Template;

	$content = $page->getHTML();
	$tpl->setTemplateString( $content );
	$tpl->parseCondition();
	$page_content = $tpl->getHTML();
	LogFile::logMe();
	return( $page_content );
	}

try
	{
	$page = new RSS;
	$page_content = processor( $page );
	}
catch( Exception $e )
	{
	$msg = $e->getMessage();
	echo $msg;
	die();
	}
header('Content-type: text/xml; charset=iso-8859-1');
echo <<<HTML
{$page_content}
HTML;
ob_end_flush();
?>